#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>

#include <netinet/in.h>
#include <netinet/ip.h>

#include <arpa/inet.h>

#include "uart.h"

struct Pack
{
    int dataType;
    int deviceId;
    char opt[16];
};

int main(int argc, char **argv)
{

    if (argc != 3)
    {
        fprintf(stderr, "set parameter error!");
        exit(EXIT_FAILURE);
    }

    // printf("hellow");

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (-1 == sockfd)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    printf("sockfd = %d\n", sockfd);

    struct sockaddr_in saddr = {0};
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(atoi(argv[1]));
    saddr.sin_addr.s_addr = inet_addr((char *)argv[2]);

    if (-1 == connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)))
    {
        perror("connect");
        exit(EXIT_FAILURE);
    }

    struct Pack buf = {0};
    buf.dataType = 1;
    buf.deviceId = 26;

    if (-1 == send(sockfd, &buf, sizeof(buf), 0))
    {
        perror("send");
        exit(EXIT_FAILURE);
    }

    int uartfd = uart_open("/dev/ttyUSB0");
    if (-1 == uartfd)
    {
        exit(EXIT_FAILURE);
    }

    if (-1 == uart_init(uartfd, 115200, 0, 8, 1, 'n'))
    {
        exit(EXIT_FAILURE);
    }

    while (1)
    {
        char buf_c[128] = {0};
        int ret = recv(sockfd, buf_c, sizeof(buf_c), 0);
        if (-1 == ret)
        {
            perror("recv");
            exit(EXIT_FAILURE);
        }

        if (0 == ret)
        {
            return 0;
        }
        printf("%s\n", buf_c);

        if (0 == strncmp(buf_c, "led on", 6))
        {
            uart_send(uartfd, "1", 1);
            memset(&buf, 0, sizeof(buf));
            buf.dataType = 3;
            buf.deviceId = 26;
            strcpy(buf.opt, "led on ok");

            printf("%s\n", buf.opt);

            send(sockfd, (char *)&buf, sizeof(buf), 0);
        }

        if (0 == strncmp(buf_c, "led off", 7))
        {
            uart_send(uartfd, "2", 1);

            memset(&buf, 0, sizeof(buf));
            buf.dataType = 3;
            buf.deviceId = 26;
            strcpy(buf.opt, "led off ok");
            printf("%s\n", buf.opt);

            send(sockfd, (char *)&buf, sizeof(buf), 0);
        }

        if (0 == memcmp(buf_c, "speaker on", 11))
        {
            uart_send(uartfd, "3", 1);

            memset(&buf, 0, sizeof(buf));
            buf.dataType = 3;
            buf.deviceId = 26;
            strcpy(buf.opt, "speaker on ok");
            printf("%s\n", buf.opt);

            send(sockfd, (char *)&buf, sizeof(buf), 0);
        }

        if (0 == memcmp(buf_c, "speaker off", 12))
        {
            uart_send(uartfd, "4", 1);

            memset(&buf, 0, sizeof(buf));
            buf.dataType = 3;
            buf.deviceId = 26;
            strcpy(buf.opt, "speaker off ok");
            printf("%s\n", buf.opt);

            send(sockfd, (char *)&buf, sizeof(buf), 0);
        }

        memset(buf_c, 0, sizeof(buf_c));
    }

    return 0;
}